package abc.gen.dao.impl;

import abc.gen.dao.SysGeneratorDao;
import abc.gen.entity.ColumnVo;
import abc.gen.entity.TableVo;
import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 代码生成器
 *
 */
@Repository("SysGeneratorDao")
public class SysGeneratorDaoImpl extends BaseDaoImpl implements SysGeneratorDao {

	@Override
	public List<Map<String, Object>> queryList(Map<String, Object> map) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables " +
				" where table_schema = (select database()) ");

		if(map.get("tableName") != null && map.get("tableName").toString().trim() !=""){
			sql.append(" and table_name like concat('%','"+map.get("tableName")+"' , '%') ");
		}
		sql.append(" order by create_time desc ");

		if(map.get("offset") != null && map.get("pageSize") != null){
			sql.append(" limit "+map.get("offset")+","+map.get("pageSize"));
		}
		int offset = Integer.parseInt(map.get("offset").toString());
		int pageSize = Integer.parseInt(map.get("pageSize").toString());

		super.pageQueryBySQL(sql.toString(),offset,pageSize);
		return null;
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(*) from information_schema.tables where table_schema = (select database())");

		if(map.get("tableName") != null && map.get("tableName").toString().trim() != ""){
			sql.append(" and table_name like concat('%','"+ map.get("tableName")+ "' , '%') ");
		}

		return super.getUniqueObject(sql.toString()).hashCode();
	}

	@Override
	public Map<String, String> queryTable(String tableName) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select table_name tableName, engine, table_comment tableComment, date_format(create_time, '%Y-%m-%d') createTime from information_schema.tables " +
				" where table_schema = (select database()) and table_name = '"+tableName +"'");
		TableVo obj = (TableVo)getUniqueObjectSql(sql.toString(),TableVo.class);
		Map<String, String>  map = new HashMap<>();
		if(obj!=null){
			map.put("tableName",obj.getTableName());
			map.put("tableComment",obj.getTableComment());
			map.put("createTime",obj.getCreateTime());
			map.put("engine",obj.getEngine());}

		return map;
	}

	@Override
	public List<Map<String, String>> queryColumns(String tableName) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns"+
				   " where table_name = '"+tableName+"' and table_schema = (select database()) order by ordinal_position ");
		List<ColumnVo> obj = (List<ColumnVo>)getListSql(sql.toString(),ColumnVo.class);

		List<Map<String, String>>  list = new ArrayList<>();

		for (ColumnVo temp:obj) {
			Map map = new HashMap();
			if(temp!=null){
				map.put("columnComment",temp.getColumnComment());
				map.put("columnKey",temp.getColumnKey());
				map.put("columnName",temp.getColumnName());
				map.put("dataType",temp.getDataType());
				map.put("extra",temp.getExtra());
			}
			list.add(map);
		}



		return list;
	}
}
